package com.demo.statemachine.d05.store;

import com.demo.statemachine.d05.orderStateMachine.OrderEvents;
import com.demo.statemachine.d05.orderStateMachine.OrderStates;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 状态机内存持久化
 */

@Component
public class OrderInMemoryStateMachinePersist implements StateMachinePersist<OrderStates, OrderEvents, String> {
    private Map<String, StateMachineContext<OrderStates, OrderEvents>> map = new HashMap<>();

    @Override
    public void write(StateMachineContext<OrderStates, OrderEvents> stateMachineContext, String key) throws Exception {
        map.put(key, stateMachineContext);
    }

    @Override
    public StateMachineContext<OrderStates, OrderEvents> read(String key) throws Exception {
        return map.get(key);
    }
}
